package controller;

import dao.GoodsDao;
import dao.OrderDao;
import dao.SupplierDao;
import dao.UserDao;
import domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.CountResult;
import util.JsonResult;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@RequestMapping(value = "/system")
public class SystemController {

    @Resource
    private UserDao userDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private SupplierDao supplierDao;
    @Resource
    private OrderDao orderDao;
    private JsonResult jsonResult= new JsonResult();

    @RequestMapping(value = "/login")
    @ResponseBody
    @Transactional
    public JsonResult login(@RequestBody Map<String,Object>map){
        User user =userDao.selectByAccount(map.get("account"));
        if(user.getPassword().equals((String)map.get("password"))){
            jsonResult.setResult("true");
            return jsonResult;
        }else {
            jsonResult.setResult("false");
            return jsonResult;
        }
    }


    @RequestMapping(value = "/register")
    @ResponseBody
    @Transactional
    public JsonResult register(@RequestBody Map<String, User>map){
        int flag;
        flag =userDao.insert(map.get("user"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }


    @RequestMapping(value = "/isRegister")
    @ResponseBody
    @Transactional
    public JsonResult isRegister(@RequestBody Map<String,Object>map){
        Registerqualification RQ =userDao.selectByTel((String)map.get("telephone"));
        if(RQ.getInvitationCode().equals((String)map.get("invitationCode"))){
            jsonResult.setResult("true");
            return jsonResult;
        }else {
            jsonResult.setResult("false");
            return jsonResult;
        }
    }
/*用户放入垃圾箱*/
    @RequestMapping(value = "/putUserToDustbin")
    @ResponseBody
    @Transactional
    public JsonResult putUserToDustbin(@RequestBody Map<String, User>map){
        int flag=userDao.update(map.get("user"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/putGoodsToDustbin")
    @ResponseBody
    @Transactional
    public JsonResult putGoodsToDustbin(@RequestBody Map<String, Goods>map){
        int flag=goodsDao.update(map.get("goods"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/putSupplierToDustbin")
    @ResponseBody
    @Transactional
    public JsonResult putSupplierToDustbin(@RequestBody Map<String, Supplier>map){
        int flag=supplierDao.update(map.get("supplier"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/putOrderToDustbin")
    @ResponseBody
    @Transactional
    public JsonResult putOrderToDustbin(@RequestBody Map<String, Order>map){
        int flag=orderDao.updateState(map.get("order"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/recoverUser")
    @ResponseBody
    @Transactional
    public JsonResult recoverUser(@RequestBody Map<String, User>map){
        int flag=userDao.update(map.get("user"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/recoverSupplier")
    @ResponseBody
    @Transactional
    public JsonResult recoverSupplier(@RequestBody Map<String, Supplier>map){
        int flag=supplierDao.update(map.get("supplier"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

    @RequestMapping(value = "/recoverOrder")
    @ResponseBody
    @Transactional
    public JsonResult recoverOrder(@RequestBody Map<String, Order>map){
        int flag=orderDao.updateState(map.get("order"));
        if (flag == 0) {
            jsonResult.setResult("false");
            return jsonResult;
        }
        else {
            jsonResult.setResult("true");
            return jsonResult;
        }
    }

@RequestMapping(value="/count")
    @ResponseBody
    @Transactional
    public CountResult count(){
        CountResult countResult= new CountResult();

        countResult.setGoodsCount(goodsDao.countGoods());
        countResult.setSupplierCount(supplierDao.countSupplier());
        countResult.setUserCount(userDao.countUser());
        countResult.setMostSaleGoodName((String)goodsDao.findMostSaleGood().get("name"));
        countResult.setMostSupplierName((String)supplierDao.findMostSupplier().get("name"));

        return countResult;
}






}
